package com.beesoft.demo.controller;

import com.beesoft.demo.dto.PageResponse;
import com.beesoft.demo.entity.Country;
import com.beesoft.demo.repository.CountryRepository;
import com.beesoft.demo.service.CountryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/countries")
@RequiredArgsConstructor
public class CountryController {

    private final CountryRepository countryRepository;
    private final CountryService countryService;

    @GetMapping
    public ResponseEntity<PageResponse<Country>> getAllCountries(
            @RequestParam(required = false) String search,
            @RequestParam(name = "order_column", required = false, defaultValue = "id") String orderColumn,
            @RequestParam(name = "order_direction", required = false, defaultValue = "asc") String orderDirection,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(name = "per_page", defaultValue = "20") int perPage) {
        
        // 验证分页参数
        if (page < 1) page = 1;
        if (perPage < 1) perPage = 20;
        if (perPage > 100) perPage = 100; // 最大每页100条
        
        PageResponse<Country> response = countryService.searchCountries(
                search, orderColumn, orderDirection, page, perPage);
        return ResponseEntity.ok(response);
    }
    
    @GetMapping("/all")
    public ResponseEntity<List<Country>> getAllCountriesWithoutPagination() {
        return ResponseEntity.ok(countryRepository.findAll());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Country> getCountryById(@PathVariable Long id) {
        return countryRepository.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/code/{code}")
    public ResponseEntity<Country> getCountryByCode(@PathVariable String code) {
        return countryRepository.findByCode(code)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    public ResponseEntity<Country> createCountry(@RequestBody Country country) {
        Country saved = countryRepository.save(country);
        return ResponseEntity.ok(saved);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Country> updateCountry(@PathVariable Long id, @RequestBody Country country) {
        return countryRepository.findById(id)
                .map(existing -> {
                    country.setId(id);
                    country.setCreatedAt(existing.getCreatedAt());
                    return ResponseEntity.ok(countryRepository.save(country));
                })
                .orElse(ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCountry(@PathVariable Long id) {
        if (countryRepository.existsById(id)) {
            countryRepository.deleteById(id);
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.notFound().build();
    }
}

